<template>
  <div class="login-page">
    <van-nav-bar class="page-nav-bar" title="登录">
      <van-icon @click="$router.go(-1)" slot="left" name="cross" />
    </van-nav-bar>

    <!-- 登录表单 -->
    <van-form @submit="onSubmit" ref="loginForm">
      <van-field
        v-model="mobile"
        name="mobile"
        placeholder="请输入手机号"
        left-icon="smile"
        :rules="loginFormRules.mobile"
        type="number"
        maxlength="11"
      >
        <i slot="left-icon" class="toutiao toutiao-shouji"></i>
      </van-field>
      <van-field
        v-model="code"
        name="code"
        placeholder="请输入验证码"
        type="number"
        maxlength="6"
        :rules="loginFormRules.code"
      >
        <i slot="left-icon" class="toutiao toutiao-yanzhengma"></i>
        <template #button>
          <van-count-down @finish="isShowCountDown = false" v-if="isShowCountDown" :time="60 * 1000" format="ss s" />
          <van-button v-else @click="sendCode" native-type="button" class="send-code-btn" size="small" type="primary">获取验证码</van-button>
        </template>
      </van-field>
      <div class="submit-login">
        <van-button class="login-btn" block type="info" native-type="submit">登录</van-button>
      </div>
    </van-form>
  </div>
</template>

<script>
import { login, sendCode } from '@/api/user.js'
import { setItem } from '@/utils/storage.js'
export default {
  data() {
    return {
      mobile: '',
      code: '',
      // 定义登陆表单的校验规则
      loginFormRules: {
        mobile: [
          { required: true, message: '请输入手机号' }, // 有没有输入
          { pattern: /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/, message: '手机号码格式错误' }
        ],
        code: [
          { required: true, message: '请输入验证码' },
          { pattern: /^\d{6}$/, message: '验证码格式错误' }
        ]
      },
      // 控制倒计时显示/隐藏
      isShowCountDown: false
    }
  },
  created() {
  },

  methods: {
    // 点击提交按钮触发
    async onSubmit() {
      // 1. 获取表单数据
      // 2. 校验表单是否合格
      // 3. 发送请求 给后台传递数据
      // await 后面必须跟 promise
      this.$toast.loading({
        duration: 0, // 持续展示 toast
        message: '加载中...',
        forbidClick: true
      })
      try {
        const res = await login({
          mobile: this.mobile,
          code: this.code
        })
        this.$store.commit('setUser', res.data)
        // 如果上面的代码出错,下面的代码不会执行,直接跳到catch
        this.$toast.success('登录成功')
        setItem('loginTime', Date.now())
      } catch (err) {
        if (err.response && err.response.status === 400) return this.$toast.fail('手机号或验证码错误')
        this.$toast.fail('系统异常,稍后重试')
      }
      // 4. 登录之后的操作
    },
    // 点击发送验证码
    async sendCode() {
      // 1. 校验手机号是否正确
      try {
        await this.$refs.loginForm.validate('mobile')
      } catch (err) {
        return console.log('校验没通过')
      }
      this.$toast.loading({
        duration: 0, // 持续展示 toast
        message: '发送中...',
        forbidClick: true
      })
      // 3. 发送请求
      try {
        await sendCode(this.mobile)
        // 2. 显示倒计时
        this.isShowCountDown = true
        this.$toast.success('发送成功')
      } catch (err) {
        if (err.response && err.response.status === 429) {
          return this.$toast('发送频繁,请1分钟后尝试')
        }
        this.$toast('系统异常')
      }
    }
  }
}
</script>

<style scoped lang='less'>
.login-page {
  .toutiao {
    font-size: 37px;
  }

  .send-code-btn {
    width: 152px;
    height: 46px;
    background: #ededed;
    border-radius: 23px;
    border: none;
    color: #666;
  }

  .submit-login {
    margin: 58px 23px;
    .login-btn {
      width: 694px;
      height: 88px;
      background: #6db4fb;
      border-radius: 10px;
      border: none;
    }
  }
}
</style>
